package com.example.android_demo01;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.android_demo01.adapter.MyAdapter;
import com.example.android_demo01.interfaces.IControl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements
        AdapterView.OnItemClickListener,
        AdapterView.OnItemLongClickListener,
        IControl {

    EditText et;
    Button quit;
    Button show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去掉标题栏
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        // 设置全屏显示
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        setContentView(R.layout.listview);

//        int[] image = {R.drawable.mail,R.drawable.skype,
//                R.drawable.windows,R.drawable.xbox};
//        String[] name = {"邮件","Skype","微软","Xbox"};
//        String[] size = {"80mb","35mb","70mb","20mb"};
//        String[] vision = {"1.0","2.0","3.0","4.0"};
//        List<Map<String,Object>> list = new ArrayList<>();
//        for(int i = 0;i < image.length; i++){
//            Map<String,Object> map = new HashMap<>();
//            map.put("logo",image[i]);
//            map.put("title",name[i]);
//            map.put("vision",vision[i]);
//            map.put("size",size[i]);
//            list.add(map);
//        }




//        // SimpleAdapetr
        // 1. 拿到listview对象
        ListView lv = findViewById(R.id.lv_main);
        // 2. 数据源
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<>();
        map.put("logo", R.drawable.laker);
        map.put("title", "洛杉矶湖人");
        map.put("version", "版本：5.21.2");
        map.put("size", "大小：35.5M");
        list.add(map);

        map = new HashMap<>();
        map.put("logo", R.drawable.rockets);
        map.put("title", "休斯顿火箭");
        map.put("version", "版本：8.21.2");
        map.put("size", "大小：75.5M");
        list.add(map);

        map = new HashMap<>();
        map.put("logo", R.drawable.dragen);
        map.put("title", "多伦多猛龙");
        map.put("version", "版本：2.21.2");
        map.put("size", "大小：15.5M");
        list.add(map);
//
//        SimpleAdapter simpleAdapter = new SimpleAdapter(
//                this,
//                list,
//                R.layout.item,
//                new String[]{"logo","title","version","size"},
//                new int[]{R.id.logo, R.id.title_id, R.id.version, R.id.size}
//        );
//
//        lv.setAdapter(simpleAdapter);

        // 自定义Adapter
        MyAdapter myAdapter = new MyAdapter(this);
        myAdapter.setList(list);
        myAdapter.setControl(this);
        lv.setAdapter(myAdapter);


        lv.setOnItemClickListener(this);
        lv.setOnItemLongClickListener(this);





//        // 1. ArrayAdapter 通过反射为listview对象
//        ListView lv = findViewById(R.id.lv_main);
//        // 2. 数据源
//        String[] data = {
//                "初始android",
//                "基础控件",
//                "入云龙公孙胜",
//                "禁军教头王进",
//                "初始android",
//                "花和尚露真身"
//        };
//        // 3. 设置适配器
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//                this,
//                android.R.layout.simple_list_item_1,
//                data
//        );
//
//        lv.setAdapter(adapter);


//        et = (EditText) findViewById(R.id.edittext_id);
//        quit = (Button) findViewById(R.id.btn_quit_id);
//        show = (Button) findViewById(R.id.btn_show_id);

//        TextView title = (TextView) findViewById(R.id.title_id);
//        title.setText(getResources().getString(R.string.app_name));
//        title.setTextColor(getResources().getColor(R.color.green));
//        Toast.makeText(this, title.getText().toString(), Toast.LENGTH_LONG).show();

//        Button btn = (Button)findViewById(R.id.btn_show_id);
        // 点击方式一
//        btn.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("ShowToast")
//            @Override
//            public void onClick(View view) {
//                String text = "你点击了按钮";
//                Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
//            }
//        });
        //点击方式二
//        btn.setOnClickListener(this);
    }


    List<Integer> multiChoiceId = new ArrayList<>();
     // 多选框
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        final String[] arr = {"熊猫", "大象", "长颈鹿", "蜘蛛侠", "无敌浩克"};
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.laker)
                .setTitle("选出你喜欢的动物")
                .setMultiChoiceItems(arr,
                        new boolean[]{false, false, false, false, false},
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
                                if(isChecked){
                                    multiChoiceId.add(position);
                                }else{
                                    multiChoiceId.remove(position);
                                }
                                Toast.makeText(MainActivity.this,
                                        "你选择的是"+multiChoiceId.toString(), Toast.LENGTH_LONG).show();
                            }
                        })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int position) {
                        Toast.makeText(MainActivity.this,
                                "你的决定是："+multiChoiceId.toString(), Toast.LENGTH_LONG).show();
                    }
                })
        .create().show();
    }



        // 点击
//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
//        Intent intent = new Intent(this, DetailActivity.class);
//        HashMap<String, Object> itemMap = (HashMap<String, Object>) parent.getItemAtPosition(position);
//        intent.putExtra("index", "" + position);
//        intent.putExtra("title", "" + itemMap.get("title"));
//        startActivity(intent);
//        Toast.makeText(this, "点击" + position, Toast.LENGTH_SHORT).show();
//    }



    // 长按
    int selected = 0;
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        //Toast.makeText(this, "长按"+position,  Toast.LENGTH_SHORT).show();

        final String[] arr = {"熊猫", "大象", "长颈鹿", "蜘蛛侠"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.rockets);
        builder.setTitle("消息提示");
        builder.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                selected = i;
                Toast.makeText(MainActivity.this, "用户选择了"+arr[selected], Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        }); // 0表示默认第一个选项
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               finish();
            }
        });
        builder.create().show();
        return true;
    }








//    @Override
//    public void onClick(View view) {
//        String text = "你点击了按钮";
//        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
//    }









    // 点击方式三
//    public void btnClick(View view){
//        String text = et.getText().toString();
//        // 响应
//        switch (view.getId()){
//            case R.id.btn_show_id:
//                text += "按钮一";
//                Log.d("spl",text);
//                break;
//            case R.id.btn_quit_id:
//                text += "按钮二";
//                Log.d("spl",text);
//                break;
//        }
//
//
//
//        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
//    }


    // 退出
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setIcon(R.drawable.rockets)
                .setTitle("确认对话框")
                .setMessage("确定要退出程序？")
                .setNegativeButton("取消", null)
                .setPositiveButton("残忍离开", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
//                        System.exit(0);
                    }
                }).show();
    }

    @Override
    public void btnClick(int position) {
        Toast.makeText(this, "item"+position, Toast.LENGTH_SHORT).show();
    }
}
